#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：HiflyVideoAPI_Test 
@File    ：TestCaseV1CreateTask.py
@Author  ：beibei
@Date    ：7/16/25 4:19 PM 
'''
import unittest
from business.voices.vo import BusinessRe
from common.assertCommon import AssertCommon
from common.yamlLoader import YamlRead
from common.caseOutput import info, class_case_decoration
from utils.utils_base import save_json_response, extract_voice_by_type_v1
from utils.utils_font import get_random_chinese_text


@class_case_decoration
class  TestCaseV1CreateTask(unittest.TestCase):
    """
    测试用例：测试v1——创建任务接口
    """
    ac = AssertCommon()
    env_config = YamlRead().env_config1()
    host = env_config['host']
    br = BusinessRe()
    authorization = env_config['Authorization']

    def testCase01_v1_create_task(self):
        """使用 audio_url + voice_id，语音驱动合成"""
        info('【step】语音驱动合成')
        url = self.host + f'/api/v1/hifly/task/create'
        body = {
            "audio_url": "https://shortvideo-cdn-dev.lingverse.co/hf/local/37/audios/10s_audios.MP3",
            "title": "自动化V1新建audio_url&video_url",
            "video_url": "https://shortvideo-cdn-dev.lingverse.co/hf/local/124/videos/118.mp4"
        }
        res = self.br.post(url, self.authorization, body)
        info('【step】断言 code == 0')
        self.ac.code_assert(0, res.json()["code"])
        save_json_response(res.json(), "data/response/task/v1/create_task_audio&video_v1.json")

    def testCase02_v1_create_task(self):
        """使用 text + voice_id，type=8，文本驱动合成"""
        info('【step】获取type=8的声音ID')
        voice_dict = extract_voice_by_type_v1(path="data/response/voice/v1/voice_list_v1.json", types=(8,))
        voice_list = voice_dict.get(8)
        assert voice_list, "voice_list_v1.json中没有type=8的声音"
        voice_id = voice_list[0]
        info('【step】文本驱动合成')
        url = self.host + f'/api/v1/hifly/task/create'
        body = {
            "text": get_random_chinese_text(30),
            "title": "自动化V1新建text + voice_id",
            "voice_id": voice_id,
            "voice_type": 8,
            "video_url": "https://shortvideo-cdn-dev.lingverse.co/hf/local/124/videos/118.mp4"
        }
        res = self.br.post(url, self.authorization, body)
        info('【step】断言 code == 0')
        self.ac.code_assert(0, res.json()["code"])
        save_json_response(res.json(), "data/response/task/v1/create_task_tts_8.json")

    def testCase03_v1_create_task(self):
        """使用 text + voice_id，type=20，文本驱动合成"""
        info('【step】获取type=20的声音ID')
        voice_dict = extract_voice_by_type_v1(path="data/response/voice/v1/voice_list_v1.json", types=(20,))
        voice_list = voice_dict.get(20)
        assert voice_list, "voice_list_v1.json中没有type=20的声音"
        voice_id = voice_list[0]
        info('【step】文本驱动合成')
        url = self.host + f'/api/v1/hifly/task/create'
        body = {
            "text": get_random_chinese_text(30),
            "title": "自动化V1新建text + voice_id",
            "voice_id": voice_id,  # 假设20是一个有效的voice_id
            "voice_type": 20,
            "video_url": "https://shortvideo-cdn-dev.lingverse.co/hf/local/124/videos/118.mp4"
        }
        res = self.br.post(url, self.authorization, body)
        info('【step】断言 code == 0')
        self.ac.code_assert(0, res.json()["code"])
        save_json_response(res.json(), "data/response/task/v1/create_task_tts_20.json")

    def testCase04_v1_create_task(self):
        """输出仅音频，type=8"""
        info('【step】输出仅音频')
        voice_dict = extract_voice_by_type_v1(path="data/response/voice/v1/voice_list_v1.json", types=(8,))
        voice_list = voice_dict.get(8)
        assert voice_list, "voice_list_v1.json中没有type=8的声音"
        voice_id = voice_list[0]
        url = self.host + f'/api/v1/hifly/task/create'
        body = {
            "text": get_random_chinese_text(30),
            "title": "自动化V1新建仅生成音频，type=8",
            "voice_id": voice_id,  # 假设8是一个有效的voice_id
            "voice_type": 8,
            "video_url": "https://shortvideo-cdn-dev.lingverse.co/hf/local/124/videos/118.mp4",
            "only_generate_audio": 1
        }
        res = self.br.post(url, self.authorization, body)
        info('【step】断言 code == 0')
        self.ac.code_assert(0, res.json()["code"])
        save_json_response(res.json(), "data/response/task/v1/create_task_onlyAudio_8_v1.json")
    def testCase05_v1_create_task(self):
        """输出仅音频，type=20"""
        info('【step】输出仅音频')
        voice_dict = extract_voice_by_type_v1(path="data/response/voice/v1/voice_list_v1.json", types=(20,))
        voice_list = voice_dict.get(20)
        assert voice_list, "voice_list_v1.json中没有type=20的声音"
        voice_id = voice_list[0]
        url = self.host + f'/api/v1/hifly/task/create'
        body = {
            "text": get_random_chinese_text(30),
            "title": "自动化V1新建仅生成音频，type=20",
            "voice_id": voice_id,  # 假设8是一个有效的voice_id
            "voice_type": 20,
            "video_url": "https://shortvideo-cdn-dev.lingverse.co/hf/local/124/videos/118.mp4",
            "only_generate_audio": 1
        }
        res = self.br.post(url, self.authorization, body)
        info('【step】断言 code == 0')
        self.ac.code_assert(0, res.json()["code"])
        save_json_response(res.json(), "data/response/task/v1/create_task_onlyAudio_20_v1.json")


    def testCase06_v1_create_task_with_invalid_data(self):
        """缺少audio_url"""
        info('【step】语音驱动合成')
        url = self.host + f'/api/v1/hifly/task/create'
        body = {
            "title": "自动化V1新建audio_url&video_url",
            "video_url": "https://shortvideo-cdn-dev.lingverse.co/hf/local/124/videos/118.mp4"
        }
        res = self.br.post(url, self.authorization, body)
        info('【step】断言 code == 14')
        self.ac.code_assert(14, res.json()["code"])


    def testCase07_v1_create_task_with_invalid_data(self):
        """缺少video_url"""
        info('【step】语音驱动合成')
        url = self.host + f'/api/v1/hifly/task/create'
        body = {
            "audio_url": "https://shortvideo-cdn-dev.lingverse.co/hf/local/37/audios/10s_audios.MP3",
            "title": "自动化V1新建audio_url&video_url"
        }
        res = self.br.post(url, self.authorization, body)
        info('【step】断言 code == 11')
        self.ac.code_assert(11, res.json()["code"])

    def testCase08_v1_create_task_with_invalid_data(self):
        """缺少text参数"""
        info('【step】获取type=8的声音ID')
        voice_dict = extract_voice_by_type_v1(path="data/response/voice/v1/voice_list_v1.json", types=(8,))
        voice_list = voice_dict.get(8)
        assert voice_list, "voice_list_v1.json中没有type=8的声音"
        voice_id = voice_list[0]
        info('【step】文本驱动合成')
        url = self.host + f'/api/v1/hifly/task/create'
        body = {
            "title": "自动化V1新建text + voice_id",
            "voice_id": voice_id,  # 假设8是一个有效的voice_id
            "voice_type": 8,
            "video_url": "https://shortvideo-cdn-dev.lingverse.co/hf/local/124/videos/118.mp4"
        }
        res = self.br.post(url, self.authorization, body)
        info('【step】断言 code == 14')
        self.ac.code_assert(14, res.json()["code"])

    def testCase09_v1_create_task(self):
        """使用 text + voice_id，type=22，文本驱动合成"""
        info('【step】获取type=8的声音ID')
        voice_dict = extract_voice_by_type_v1(path="data/response/voice/v1/voice_list_v1.json", types=(22,))
        voice_list = voice_dict.get(22)
        assert voice_list, "voice_list_v1.json中没有type=8的声音"
        voice_id = voice_list[0]
        info('【step】文本驱动合成')
        url = self.host + f'/api/v1/hifly/task/create'
        body = {
            "text": get_random_chinese_text(30),
            "title": "自动化V1新建text + voice_id",
            "voice_id": voice_id,
            "voice_type": 22,
            "video_url": "https://shortvideo-cdn-dev.lingverse.co/hf/local/124/videos/118.mp4"
        }
        res = self.br.post(url, self.authorization, body)
        info('【step】断言 code == 0')
        self.ac.code_assert(0, res.json()["code"])
        save_json_response(res.json(), "data/response/task/v1/create_task_tts_8.json")
